﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using System.Runtime.Serialization.Formatters.Binary;

namespace DoubleH.Configuration
{
    public sealed class ConfigSerializer
    {
        public static AppConfig LoadAppConfig()
        {
            AppConfig tConfig = null;
            XmlSerializer xmlSerial = new XmlSerializer(typeof(AppConfig));

            StreamReader fs = null;
            try
            {
               
                fs = new StreamReader(AppDefines.appConfigFile, Encoding.UTF8);
                //fs = new FileStream(AppDefines.appConfigFile, FileMode.Open, FileAccess.Read, FileShare.Read);
                tConfig = (AppConfig)xmlSerial.Deserialize(fs);
                fs.Dispose();
            }
            catch (Exception) { tConfig = new AppConfig(); }
            fs = null;
            return tConfig;
        }
        public static UserConfig LoadUserConfig(Int64 userId)
        {
            UserConfig tConfig = null;
            XmlSerializer xmlSerial = new XmlSerializer(typeof(UserConfig));

            FileStream fs = null;
            try
            {
                fs = new FileStream(AppDefines.appConfigPath+userId.ToString()+".xml", FileMode.Open, FileAccess.Read, FileShare.Read);
                tConfig = (UserConfig)xmlSerial.Deserialize(fs);
                fs.Dispose();
            }
            catch (Exception) { tConfig = new UserConfig(); }
            fs = null;
            return tConfig;

            //数据库存储
            //UserConfig tConfig = null;
            //XmlSerializer xmlSerial = new XmlSerializer(typeof(UserConfig));
            //try
            //{
            //    MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(FCNS.Data.Table.UserS.LoginUser.UserConfig));
            //    tConfig = (UserConfig)xmlSerial.Deserialize(ms);
            //    ms.Dispose();
            //}
            //catch (Exception) { tConfig = new UserConfig(); }
            //return tConfig;
        }


        public static bool SaveConfig(AppConfig appConfigEx)
        {
            XmlSerializer xmlSerial = new XmlSerializer(typeof(AppConfig));

            StreamWriter fs = new StreamWriter(AppDefines.appConfigFile,false, Encoding.UTF8);
            //Stream fs = new FileStream(AppDefines.appConfigFile, FileMode.Create);
            FileInfo fi = new FileInfo(AppDefines.appConfigFile);
            
            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Encoding = Encoding.UTF8;
            xws.Indent = true;
            xws.IndentChars = "\t";

            XmlWriter xw = XmlWriter.Create(fs, xws);
            xmlSerial.Serialize(xw, appConfigEx);
            xw.Close();
            if (fs != null) { fs.Close(); fs = null; }
            return true;
        }
        public static bool SaveConfig(UserConfig userConfigEx, Int64 userId)
        {
            XmlSerializer xmlSerial = new XmlSerializer(typeof(UserConfig));
            Stream fs = new FileStream(AppDefines.appConfigPath + userId.ToString() + ".xml", FileMode.Create);

            XmlWriterSettings xws = new XmlWriterSettings();
            xws.Encoding = new UTF8Encoding(true);
            xws.Indent = true;
            xws.IndentChars = "\t";

            XmlWriter xw = XmlWriter.Create(fs, xws);
            xmlSerial.Serialize(xw, userConfigEx);
            xw.Close();
            if (fs != null) { fs.Close(); fs = null; }
            return true;

            //XmlSerializer xmlSerial = new XmlSerializer(typeof(UserConfig));
            //MemoryStream ms = new MemoryStream();

            //XmlWriterSettings xws = new XmlWriterSettings();
            //xws.Encoding = new UTF8Encoding(false);
            //xws.Indent = true;
            //xws.IndentChars = "\t";
            //xws.OmitXmlDeclaration = false;

            //XmlWriter xw = XmlWriter.Create(ms, xws);
            //xmlSerial.Serialize(xw, userConfigEx);
            //xw.Close();
            //FCNS.Data.Table.UserS.LoginUser.UserConfig = Encoding.UTF8.GetString(ms.GetBuffer());
            //ms.Dispose();
            //return true;
        }
    }
}